﻿#region license
// CopyRight (c) 2009-2010 HangJing Teconology. All Rights Reserved.
// Function :礼品信息列表
// Created by tuhui at 2010-6-24 16:28:03.
// E-Mail: tuhui@ihangjing.com
#endregion
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.Common;
using Hangjing.Cache;

/// <summary>
/// 装备列表
/// </summary>
public partial class qy_54tss_Admin_deviceList : System.Web.UI.Page
{
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? string.Empty : o.ToString();
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    Devices dal = new Devices();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            WebUtility.SetDDLCity(tbcityid);

            WebUtility.BindList("id", "classname", CacheHelper.GetDeviceSorts().ToList(), tbsortid);
            SqlWhere = new RightsTool().getDevicesWhere(SqlWhere);
            BindData();
        }
    }

    private void BindData()
    {
        this.AspNetPager1.RecordCount = dal.GetCount(SqlWhere);
        this.rtpGifts.DataSource = dal.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere, "did", 1);
        this.rtpGifts.DataBind();
        AlertScript.RegScript(this.Page, this.UpdatePanel1, "init();");
       
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        SqlWhere = " 1= 1 ";
        SqlWhere = new RightsTool().getDevicesWhere(SqlWhere);
        if (WebUtility.InputText(tbKeyword.Text.Trim()) != "")
        {
            SqlWhere += " and  title  like '%" + Utils.RegEsc(WebUtility.InputText(this.tbKeyword.Text.Trim())) + "%' ";
        }
        if (WebUtility.InputText(tbsn.Text.Trim()) != "")
        {
            SqlWhere += " and  devNO  like '%" + Utils.RegEsc(WebUtility.InputText(this.tbsn.Text.Trim())) + "%' ";
        }


        if (this.tbcityid.SelectedValue != "-1")
        {
            SqlWhere += " and  cityid ='" + tbcityid.SelectedValue + "'";
        }
        if (this.tbsortid.SelectedValue != "-1")
        {
            SqlWhere += " and  sortid ='" + tbsortid.SelectedValue + "'";
        }
        if (this.ddlop.SelectedValue != "-1")
        {
           
                decimal value = 0;
                try
                {
                    value = Convert.ToDecimal(tbfirst.Text);
                }
                catch (Exception)
                {
                    AlertScript.AjaxRegisterScript("alert('条件参数错误，请输入数字','text:操作失败!','250','150','true','2000','true','text');", Page);
                    return;
                }

            SqlWhere += " and stock " + ddlop.SelectedValue + " " + value + "";

        }
        this.AspNetPager1.RecordCount = dal.GetCount(SqlWhere);
        BindData();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    protected void DelList_Click(object sender, EventArgs e)
    {
        string IdList = this.hidDels.Value;
        if (dal.DelList(IdList) > 0)
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('删除成功','success','true',5);init();");
            BindData();
        }
        else
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('删除失败','error','true',5);init();");
        }
    }

    protected string ParseState(object x)
    {
        string temp = x.ToString();
        string rs = "";
        switch (temp)
        {
            case "0":
                rs = "休息";
                break;
            case "1":
                rs = "<span style='color:green'>上班</span>";
                break;
        }
        return rs;
    }
    protected void rtpGifts_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (dal.DelDevices(Convert.ToInt32(e.CommandArgument)) > 0 )
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('删除成功','success','true',5);init();");
                BindData();
            }
            else
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('删除失败','error','true',5);init();");
            }
        }
    }

    protected void btExport_Click(object sender, EventArgs e)
    {
        //判断权限
        int _rs = WebUtility.checkOperator_super(7);
        if (_rs == 0)
        {
            AlertScript.RegScript(this.Page, UpdatePanel1,"alert('无操作权限','success','true',5);init();");
            return;
        }

        WebUtility.FixsetCookie("webStatisticsExcleOrder", Server.UrlEncode(SqlWhere), 1);
        Response.Redirect("deviceToExcel.aspx");
    }
}
